package com.reyco1.display.container
{
	import com.reyco1.display.SmartSprite;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	
	public class Container extends SmartSprite
	{
		public function Container()
		{
			super();
		}
		
		public function invalidate():void
		{
			startRender();
		}
		
		override protected function onRender():void
		{
			stopRender();
			build();
		}
		
		protected function build():void
		{		
			// to override
			dispatchEvent(new Event(Event.RESIZE));
		}
		
		override public function addChild(child:DisplayObject):DisplayObject
		{
			super.addChild(child);
			child.addEventListener(Event.RESIZE, handleChildResize);
			build();
			return child;
		}
		
		override public function addChildAt(child:DisplayObject, index:int):DisplayObject
		{
			super.addChildAt(child, index);
			child.addEventListener(Event.RESIZE, handleChildResize);
			build();
			return child;
		}
		
		override public function removeChild(child:DisplayObject):DisplayObject
		{
			super.removeChild(child);
			child.removeEventListener(Event.RESIZE, handleChildResize);
			build();
			return child;
		}
		
		override public function removeChildAt(index:int):DisplayObject
		{
			var child:DisplayObject = super.removeChildAt(index);
			child.removeEventListener(Event.RESIZE, handleChildResize);
			build();
			return child;
		}
		
		private function handleChildResize(e:Event):void
		{
			build();
		}
	}
}